﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageConfigtable.Queries
{
    public class GetConfigTableByIdQueries : IRequest<Json<GetconfigtableDTO>>
    {
        public int id { get; set; }
    }

    public class GetConfigTableByIdQueriesHandler : IRequestHandler<GetConfigTableByIdQueries, Json<GetconfigtableDTO>>
    {
        private readonly ICurrentUserService _icurrentUserTable;
        private readonly IConfigtableRepository _iconfigtableRepository;
        public GetConfigTableByIdQueriesHandler(ICurrentUserService icurrentUserTable, IConfigtableRepository iconfigtableRepository)
        {
            _icurrentUserTable = icurrentUserTable;
            _iconfigtableRepository = iconfigtableRepository;
        }

        public async Task<Json<GetconfigtableDTO>> Handle(GetConfigTableByIdQueries request, CancellationToken cancellationToken)
        {
            GetconfigtableDTO model = new GetconfigtableDTO() { };
            if (request.id <= 0)
            {
                return Json<GetconfigtableDTO>.Failed("请选择需要修改的配置", model);
            }
            else
            {
                int userid = _icurrentUserTable.GetUserId();
                var result = await _iconfigtableRepository.GetConfigTableById(request.id, userid, cancellationToken);
                if (result != null && result.id > 0)
                {
                    model = new GetconfigtableDTO() { id = result.id, capacitythreshold = result.capacitythreshold, type = result.type ?? 0 };
                    return Json<GetconfigtableDTO>.Success("查询成功", model);

                }
                else
                {
                    return Json<GetconfigtableDTO>.Failed("请选择有效的配置", model);
                }
            }
        }
    }
}
